<template>
  <div class="mainbox">
    
    <el-tabs v-model="activeName" class="demo-tabs"  @click="handleClick">
    <el-tab-pane label="待处理" name="first" >

    </el-tab-pane>

    <el-tab-pane label="已处理" name="second" >

    </el-tab-pane>

    <el-tab-pane label="全部订单" name="third" >
    
    </el-tab-pane>
    <el-card class="page-container">
          <el-table :data="orders" style="width: 100%">
            <el-table-column label="序号" width="100" type="index">
            </el-table-column>
            <el-table-column label="orderId" prop="orderId"></el-table-column>
            <el-table-column label="userName" prop="username"></el-table-column>
            <el-table-column label="orderDate" prop="orderDate"></el-table-column>
            <el-table-column label="操作" width="100">
              <template #default="{ row }" >
                <el-button type="primary" @click="showDetils(row)" :icon="Check" circle ></el-button>
                <el-button type="danger" @click="delOrder(row)" :icon="Delete" circle />
              </template>
            </el-table-column>
            <template #empty>
              <el-empty description="没有数据" />
            </template>
          </el-table>
        </el-card>
    </el-tabs>

    <el-drawer v-model="drawer" title="订单详情" :with-header="false" size="50%">
      <el-form :model="orderModel" label-width="100px">
        <el-form-item label="订单id">
              <el-input
                v-model="orderModel.orderId"
                
              ></el-input>
            </el-form-item>
            <el-form-item label="买家ID">
              <el-input
                v-model="orderModel.username"
              ></el-input>
            </el-form-item>
            <el-form-item label="花费合计">
              <el-input
                v-model="orderModel.totalPrice"
              ></el-input>
            </el-form-item>
            <el-form-item label="address1">
              <el-input
                v-model="orderModel.shipAddress1"
              ></el-input>
            </el-form-item>
            <el-form-item label="address2">
              <el-input
                v-model="orderModel.shipAddress2"
              ></el-input>
            </el-form-item>
            <el-form-item label="shipZip">
              <el-input
                v-model="orderModel.shipZip"
              ></el-input>
            </el-form-item>
            <el-form-item label="billAddress1">
              <el-input
                v-model="orderModel.billAddress1"
              ></el-input>
            </el-form-item>
            <el-form-item label="billAddress2">
              <el-input
                v-model="orderModel.billAddress2"
              ></el-input>
            </el-form-item>
            <el-form-item label="billToFirstName">
              <el-input
                v-model="orderModel.billToFirstName"
              ></el-input>
            </el-form-item>
            <el-form-item label="billToLastName">
              <el-input
                v-model="orderModel.billToLastName"
              ></el-input>
            </el-form-item>
            <el-form-item label="billZip">
              <el-input
                v-model="orderModel.billZip"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button v-if="activeName=='first'" type="primary" @click="address">确认发货</el-button>
              <el-button type="info" @click="drawer=false">退出</el-button>
            </el-form-item>
          </el-form>
   </el-drawer>

    
    
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import type { TabsPaneContext } from 'element-plus'
import { Edit, Delete,Search,Check } from "@element-plus/icons-vue";
import { ElMessageBox, ElMessage } from "element-plus";
import { 
  getPeddingOrdersService,
  getAllOrdersService,
  getSolvedOrdersService,
  delOrderService,
  deliOrderService
 } from '@/api/order';

const drawer = ref(false)

//相关数据模型
const orderModel = ref({
    
      orderId: 0,
      username: "string",
      orderDate: "string",
      shipAddress1: "string",
      shipAddress2: "string",
      shipCity: "string",
      shipState: "string",
      shipZip: "string",
      shipCountry: "string",
      billAddress1: "string",
      billAddress2: "string",
      billCity: "string",
      billState: "string",
      billZip: "string",
      billCountry: "string",
      courier: "string",
      totalPrice: "string",
      billToFirstName: "string",
      billToLastName: "string",
      shipToFirstName: "string",
      shipToLastName: "string",
      creditCard: "string",
      expiryDate: "string",
      cardType: "string",
      locale: "string"
})

const orders = ref([
  {
      orderId: 0,
      username: "string",
      orderDate: "string",
      shipAddress1: "string",
      shipAddress2: "string",
      shipCity: "string",
      shipState: "string",
      shipZip: "string",
      shipCountry: "string",
      billAddress1: "string",
      billAddress2: "string",
      billCity: "string",
      billState: "string",
      billZip: "string",
      billCountry: "string",
      courier: "string",
      totalPrice: "string",
      billToFirstName: "string",
      billToLastName: "string",
      shipToFirstName: "string",
      shipToLastName: "string",
      creditCard: "string",
      expiryDate: "string",
      cardType: "string",
      locale: "string"
  },
])

//得到orders
const allOrders=async()=>{
  let result= await getAllOrdersService();
  console.log("allorder's result"+result)
  orders.value = result.data.data;
  console.log("allorders:"+orders.value)
}
const peddingOrders=async()=>{
  let result= await getPeddingOrdersService();
  console.log("peddingorder's result"+result)
  orders.value = result.data.data;
}
const solvedOrders=async()=>{
  let result= await getSolvedOrdersService();
  console.log("solveorder's result"+result)
  orders.value = result.data.data;
}

//默认
peddingOrders();

 //标签页
 const activeName = ref('first')

const handleClick = () => {
  if(activeName.value==='first'){
  peddingOrders();}
  else if(activeName.value==='second'){
  solvedOrders();}
  else if(activeName.value==='third'){
    allOrders();
  }
  //console.log(orders.value)
}



//删除
const delOrder =(row)=>{
  const id = Number(row.orderId);
  ElMessageBox.confirm(
    '确认删除?',
    '温馨提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async() => {
      let result = await delOrderService(id)
      ElMessage({
        type: 'success',
        message: '删除成功',
      })
      //刷新
      peddingOrders();
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '操作已取消',
      })
    })
}

//展示详情
const showDetils = (row)=>{
  drawer.value=true;
  orderModel.value =row;
  console.log("row:"+row)
}

//处理
const address=()=>{
  ElMessageBox.confirm(
    '确认发货?',
    '温馨提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async() => {
      let id = orderModel.value.orderId
      let result = await deliOrderService(id)
      drawer.value = false
      ElMessage({
        type: 'success',
        message: '发货成功',
      })
      //刷新
      peddingOrders();
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '操作已取消',
      })
    })
}
</script>

<style>
.demo-tabs > .el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}
</style>